package net.anzix.jhacks.flexclipse.builder;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.anzix.jhacks.flexclipse.Activator;
import net.anzix.jhacks.flexclipse.properties.ProjectPropertyLoader;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.osgi.framework.Bundle;

import flex2.tools.oem.Application;
import flex2.tools.oem.Configuration;

public class FlexclipseBuilder extends IncrementalProjectBuilder {

	public static final String BUILDER_ID = "Flexclipse.flexclipseBuilder";
	static final String MARKER_TYPE = "Flexclipse.mxmlProblem";

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.internal.events.InternalBuilder#build(int,
	 *      java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@SuppressWarnings("unchecked")
	protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
			throws CoreException {
		fullBuild(monitor);
		return null;
	}

	protected void fullBuild(final IProgressMonitor monitor)
			throws CoreException {
		try {
			
			final ProjectPropertyLoader loader = new ProjectPropertyLoader(getProject());
			
			getProject().deleteMarkers(IMarker.PROBLEM, true,
					IResource.DEPTH_INFINITE);
			final Application application = new Application(new File(getProjectHome()
					+ "/" + loader.getMainMxml()));
			final Configuration configuration = application.getDefaultConfiguration();
			configuration.addConfiguration(new File(getFlexHome()
					+ "/frameworks/flex-config.xml"));
			final IFile file = getProject().getFile(loader.getOutput());
			application.setOutput(new File(getProjectHome()
					+ loader.getOutput()));
			application.setLogger(new ConsoleLogger(getProject()));
			application.setProgressMeter(new FlexBuildProgressMeter(monitor));
			final String defaultCss = loader.getDefaultCSS();
			if(defaultCss != null) {
				application.getConfiguration().setDefaultCSS(new File(getProjectHome() + defaultCss));
			}
			final long result = application.build(true);
			file.refreshLocal(IResource.DEPTH_ONE, monitor);
			if (result < 0) {
				final IMarker buildFailedMarker = getProject().createMarker(
						MARKER_TYPE);
				buildFailedMarker.setAttribute(IMarker.SEVERITY,
						IMarker.SEVERITY_ERROR);
				buildFailedMarker.setAttribute(IMarker.MESSAGE,
						"Build failed, see details!");
			}
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private String getFlexHome() throws IOException {
		final Bundle bundle = Activator.getDefault().getBundle();
		return (FileLocator.toFileURL((FileLocator.find(bundle, new Path(
				"flexsdk"), new HashMap())))).getPath();
	}

	private String getProjectHome() {
		return getProject().getLocation().toOSString().concat("/");
	}

}
